package com.danhar.forward.util;


import static com.danhar.forward.util.ResponseCode.SUCCEED_CODE;

/**
 * 返回统一对象
 *
 * @param
 * @author lixiaoyang
 * @date 2020/04/14  13:35
 * @return
 */

public class ResponseEntity<T> {
    private Integer code;
    private String msg;
    private T data;
    private Long expired_at;


    public static <T> ResponseEntity<T> ok() {
        return restResult(null, SUCCEED_CODE, null);
    }

    public static <T> ResponseEntity<T> ok(String msg) {
        return restResult(null, SUCCEED_CODE, msg);
    }

    public static <T> ResponseEntity<T> ok(T data) {
        return restResult(data, SUCCEED_CODE, null);
    }

    public static <T> ResponseEntity<T> ok(T data, String msg) {
        return restResult(data, SUCCEED_CODE, msg);
    }


    public static <T> ResponseEntity<T> response(ResponseCode responseCode) {
        return restResult(null, responseCode, null);
    }

    public static <T> ResponseEntity<T> response(ResponseCode responseCode, String msg) {
        return restResult(null, responseCode, msg);
    }

    public static <T> ResponseEntity<T> response(ResponseCode responseCode, T data) {
        return restResult(data, responseCode, null);
    }

    public static <T> ResponseEntity<T> response(ResponseCode responseCode, T data, String msg) {
        return restResult(data, responseCode, msg);
    }

    private static <T> ResponseEntity<T> restResult(T data, ResponseCode responseCode, String inputMsg) {
        ResponseEntity<T> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(responseCode.getCode());
        responseEntity.setData(data);
        if (inputMsg == null) {
            responseEntity.setMsg(responseCode.getMsg());
        } else {
            responseEntity.setMsg(inputMsg);
        }
        responseEntity.setExpired_at(System.currentTimeMillis());
        return responseEntity;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Long getExpired_at() {
        return expired_at;
    }

    public void setExpired_at(Long expired_at) {
        this.expired_at = expired_at;
    }

    @Override
    public String toString() {
        return "ResponseEntity{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", expired_at=" + expired_at +
                '}';
    }
}
